<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 4] 4.7 Shift Operators</TITLE>
<META NAME="author" CONTENT="Mark Grand">
<META NAME="date" CONTENT="Thu Jul 31 13:10:21 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="java">
<META NAME="title" CONTENT="Java Language Reference">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java Language Reference" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch04_06.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 4<br>Expressions</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch04_08.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JLR2-CH-4-SECT-7">4.7 Shift Operators</A></h2>

<P CLASS=para>
<A NAME="CH04.SHIFT1"></A><A NAME="CH04.SHIFT3"></A><A NAME="CH04.SHIFT2"></A>The shift operators in
Java are used for left shift (<tt CLASS=literal>&lt;&lt;</tt>), right
shift (<tt CLASS=literal>&gt;&gt;</tt>), and unsigned right shift (<tt CLASS=literal>&gt;&gt;&gt;</tt>)
operations. The shift operators may appear in a shift expression:


<p>
<img align=middle src="./figs/jlr0417.gif" alt="[Graphic: Figure from the text]" width=424 height=99 border=0>

<P CLASS=para>
The shift operators are equal in precedence and are evaluated from left
to right.

<P CLASS=para>
<b>References</b>
<A HREF="ch04_06.htm#JLR2-CH-4-SECT-6">Additive Operators</A>;
<A HREF="ch04_14.htm#JLR2-CH-4-SECT-14">Order of Operations</A>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JLR2-CH-4-SECT-7.1">Left Shift Operator &lt;&lt;</A></h3>

<P CLASS=para>
The left shift operator <tt CLASS=literal>&lt;&lt;</tt>
produces a pure value that is its left operand left-shifted by the
number of bits specified by its right operand. The <tt CLASS=literal>&lt;&lt;</tt>
operator may appear in a shift expression. The left shift operator
never throws an exception.

<P CLASS=para>
Here are some examples of
the left shift operator:

<DIV CLASS=screen>
<P>
<PRE>
(3&lt;&lt;2) == 12
(-3&lt;&lt;2) == -12
(0x01234567&lt;&lt;4) == 0x12345670
(0xF1234567&lt;&lt;4) == 0x12345670
</PRE>
</DIV>

<P CLASS=para>
The type of each operand of
the left shift operator must be an integer data type, or a compile-time
error occurs. The <tt CLASS=literal>&lt;&lt;</tt> operator may perform
type conversions on its operands; unlike arithmetic binary operators,
each operand is converted independently. If the type of an operand
is <tt CLASS=literal>byte</tt>, <tt CLASS=literal>short</tt>, or <tt CLASS=literal>char</tt>,
that operand is converted to an <tt CLASS=literal>int</tt> before the
value of the operator is computed. The type of the value produced
by the left shift operator is the type of its left operand.

<P CLASS=para>
If the converted type of the left operand is <tt CLASS=literal>int</tt>,
only the five least significant bits of the value of the right operand
are used as the shift distance. Therefore, the shift distance is
in the range 0 through 31.
In this case, the value produced by <tt CLASS=literal>r
&lt;&lt; s</tt> is mathematically equivalent to:

<P CLASS=blockquote><BLOCKQUOTE><P>
<P CLASS=para>
<img src='figs/eq4_2.gif' border=0>
</BLOCKQUOTE><P>
<P CLASS=para>
If the type of the left operand is <tt CLASS=literal>long</tt>,
only the six least significant bits of the value of the right operand
are used as the shift distance. Therefore, the shift distance is
in the range 0 through 63. In this case, the value produced by <tt CLASS=literal>r
&lt;&lt; s</tt> is mathematically equivalent to:

<P CLASS=blockquote><BLOCKQUOTE><P>
<P CLASS=para>
<img src='figs/eq4_3.gif' border=0>
</BLOCKQUOTE><P>
<P CLASS=para>
<b>References</b>
<A HREF="ch03_01.htm#JLR2-CH-3-SECT-1.1.1">Integer types</A>

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JLR2-CH-4-SECT-7.2">Right Shift Operator &gt;&gt;</A></h3>

<P CLASS=para>
The right shift operator <tt CLASS=literal>&gt;&gt;</tt>
produces a pure value that is its left operand right-shifted with
sign extension by the number of bits specified by its right operand.
Right-shifting with sign extension means that shifting a value
<tt CLASS=literal>n</tt> places to the right causes the <tt CLASS=literal>n</tt>
high order
bits to contain the same value as the sign bit of the unshifted
value. The &gt;&gt; operator may appear as part of a shift expression.
The right shift operator never throws an exception.
Here are some examples of the right shift operator:

<DIV CLASS=screen>
<P>
<PRE>
(0x01234567&gt;&gt;4) == 0x00123456
(0xF1234567&gt;&gt;4) == 0xFF123456
</PRE>
</DIV>

<P CLASS=para>
The type of each operand of
the right shift operator must be an integer data type, or a compile-time
error occurs. The <tt CLASS=literal>&gt;&gt;</tt> operator may perform type
conversions on its operands; unlike arithmetic binary operators, each
operand is converted independently. If the type of an operand is
<tt CLASS=literal>byte</tt>, <tt CLASS=literal>short</tt>, or <tt CLASS=literal>char</tt>,
that operand is converted to an <tt CLASS=literal>int</tt> before the
value of the operator is computed. The type of the value produced
by the right shift operator is the type of its left operand.

<P CLASS=para>
If the converted type of the left operand is <tt CLASS=literal>int</tt>,
only the five least significant bits of the value of the right operand
are used as the shift distance. Therefore, the shift distance is
in the range 0 through 31.

<P CLASS=para>
In this case, the value produced by <tt CLASS=literal>r
&gt;&gt; s</tt> is mathematically equivalent to:

<P CLASS=blockquote><BLOCKQUOTE><P>
<P CLASS=para>
<img src='figs/eq4_4.gif' border=0>
</BLOCKQUOTE><P>
<P CLASS=para>
The notation <img src='figs/floorx.gif' border=0> means
the greatest integer less than or equal to <I CLASS=emphasis>x</I> ;
this is called the floor operation.

<P CLASS=para>
If the type of the
left operand is <tt CLASS=literal>long</tt>, only the six least significant
bits of the value of the right operand are used as the shift distance.
Therefore, the shift distance is in the range 0 through 63. In this
case, the value produced by <tt CLASS=literal>r &gt;&gt; s</tt> is mathematically
equivalent to:

<P CLASS=blockquote><BLOCKQUOTE><P>
<P CLASS=para>
<img src='figs/eq4_5.gif' border=0>
</BLOCKQUOTE><P>
<P CLASS=para>
<b>References</b>
<A HREF="ch03_01.htm#JLR2-CH-3-SECT-1.1.1">Integer types</A>

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JLR2-CH-4-SECT-7.3">Unsigned Right Shift Operator &gt;&gt;&gt;</A></h3>

<P CLASS=para>
The unsigned right shift operator <tt CLASS=literal>&gt;&gt;&gt;</tt>
produces a pure
value that is its left operand right-shifted with zero extension
by the number of bits specified by its right operand. Right-shifting
with zero extension means that shifting a value <tt CLASS=literal>n</tt>
places to the right causes the <tt CLASS=literal>n</tt> high order
bits to contain zero. The <tt CLASS=literal>&gt;&gt;&gt;</tt> operator may appear
as part of a shift expression. The unsigned right shift operator
never throws an exception.

<P CLASS=para>
Here are some examples of
the unsigned right shift operator:

<DIV CLASS=screen>
<P>
<PRE>
(0x01234567&gt;&gt;&gt;4) == 0x00123456
(0xF1234567&gt;&gt;&gt;4) == 0x0F123456
</PRE>
</DIV>

<P CLASS=para>
The type of each operand of
the unsigned right shift operator must be an integer data type,
or a compile-time error occurs. The <tt CLASS=literal>&gt;&gt;&gt;</tt> operator
may perform type conversions on its operands; unlike arithmetic binary
operators, each operand is converted independently. If the type
of an operand is <tt CLASS=literal>byte</tt>, <tt CLASS=literal>short</tt>,
or <tt CLASS=literal>char</tt>, that operand is converted to an <tt CLASS=literal>int</tt>
before the value of the operator is computed. The type of the value
produced by the unsigned right shift operator is the type of its
left operand.
If the converted type of the left operand
is <tt CLASS=literal>int</tt>, only the five least significant bits
of the value of the right operand are used as the shift distance.
So, the shift distance is in the range 0 through 31. Here,
the value produced by <tt CLASS=literal>r &gt;&gt;&gt; s</tt> is the same as:

<DIV CLASS=screen>
<P>
<PRE>
s==0 ? r : (r &gt;&gt; s) &amp; ~(-1&lt;&lt;(32-s))
</PRE>
</DIV>

<P CLASS=para>
If the type of the left operand is <tt CLASS=literal>long</tt>,
then only the six least significant bits of the value of the right operand
are used as the shift distance. So, the shift distance is
in the range 0 through 63.
Here, the value produced by <tt CLASS=literal>r
&gt;&gt;&gt; s</tt> is the same as the following:

<DIV CLASS=screen>
<P>
<PRE>
s==0 ? r : (r &gt;&gt; s) &amp; ~(-1&lt;&lt;(64-s))
</PRE>
</DIV>

<P CLASS=para>
<b>References</b>
<A HREF="ch03_01.htm#JLR2-CH-3-SECT-1.1.1">Integer types</A>

</DIV>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch04_06.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch04_08.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Additive Operators</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>Relational Comparison Operators</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
